<?php

namespace Home\Model;
use Think\Model;

class ServiceRecommendModel extends Model {
	private $recommend_table = 'dmp_service_recommend';
	private $services_table = 'dmp_services';
	private $image_api_table = 'dmp_service_apis_image';

	//精品，推荐，热门数据
	public function recommendList($type = 1,$limit = 10){
		try{
			$key = md5("recommend_list_kk_".$type."_".$limit);
			$list = S($key);
			if($list === false){
				$sql = "select ser.*,u.auth_type,rec.sort_id,ifnull(least(min(api.min_price),if(api.api_is_charge=0,0,99999999)),0) price,group_concat(DISTINCT tag.name) tag_string from ".$this->recommend_table ." rec join ".$this->services_table."  ser on ser.service_id= rec.service_id and ser.service_status=1 left join ".$this->image_api_table." api on api.service_id=ser.service_id and api.api_status>0 left join dmp_service_tag ser_tag on ser_tag.service_id=ser.service_id left join dmp_tag tag on tag.id=ser_tag.tag_id left join dmp_users u on u.id=ser.uid where rec.service_type_id=".(int) $type ." group by rec.service_id order by rec.sort_id asc limit ".$limit;

				$list = $this->query($sql);

				S($key,$list,6*3600);
			}
		}catch(exception $e){
			return null;
		}

		return $list;
	}
	
	//分类列表页中，获取所有精品或热门数据，存在分页，排序（按照服务调用次数或服务名称）
	public function getRecommendListWithOrder($type = 1,$order_type = 1,$page = 1,$pagesize = 20){
		//默认排序，按照服务英文名称正向排；按照调用次数倒排；（注意，服务状态，是否有上线api）
		switch($order_type){
			case 2:
				$order_string = " order by s.call_num desc ";
				break;
			default:
				$order_string = " order by s.service_name_en asc ";
				break;
		}

		//todo 缓存
		$key = md5("serviceRecommend_list_withorder_".$type."_".$order_type."_".$page."_".$pagesize);
		$list = S($key);
		if($list === false){
			$count_sql = "select s.service_name,s.service_name_en,s.call_num,s.service_publisher,count(image.service_id) num,s.service_logo,s.service_introduction,s.uid,u.auth_type,s.service_id,ifnull(least(min(image.min_price),if(image.api_is_charge=0,0,99999999) ),0) price,group_concat(DISTINCT tag.name) tag_string "; 
			$count_sql .=	" from dmp_service_recommend r left join dmp_services s on r.service_id=s.service_id left join dmp_service_apis_image image on image.service_id=s.service_id LEFT JOIN dmp_users u on u.id=image.uid ";
			$count_sql .= " left join dmp_service_tag ser_tag on ser_tag.service_id=s.service_id left join dmp_tag tag on tag.id=ser_tag.tag_id ";
			$count_sql .=" where r.service_type_id= ".$type." and s.service_status=1 and s.service_enable=0 and image.api_status=1 and image.api_auth_status=2 "; 
			if($category_id && is_numeric($category_id)) $count_sql .=" and s.category_id= ".$category_id ;
			$count_sql .=" group by image.service_id HAVING num >0 ";

			$sql = $count_sql.$order_string;
			$all_list = $this->query($sql);
			$total_num = count($all_list);

			$start = ($page-1)*$pagesize;
			$list = array_slice($all_list, $start, $pagesize);

			S($key,$list,3600);
		}

		$data['total_num'] = $total_num;
		$data['list'] = $list;
		return $data;
	}

}

?>